package com.encryption.dao;

import java.math.BigInteger;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.encryption.logic.impl.ConditionSqlParser;
import com.encryption.logic.impl.FileLogConditionSqlParser;
import com.encryption.model.FileLog;

/**
 * A data access object (DAO) providing persistence and search support for
 * FileLog entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.encryption.model.FileLog
 * @author MyEclipse Persistence Tools
 */

public class FileLogDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(FileLogDAO.class);

	// property constants

	public void save(FileLog transientInstance) {
		log.debug("saving FileLog instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(FileLog persistentInstance) {
		log.debug("deleting FileLog instance");
		try {
			getSession().delete(persistentInstance);
			getSession().flush();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public FileLog findById(java.lang.Integer id) {
		log.debug("getting FileLog instance with id: " + id);
		try {
			FileLog instance = (FileLog) getSession().get(
					"com.encryption.model.FileLog", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(FileLog instance) {
		log.debug("finding FileLog instance by example");
		try {
			List results = getSession()
					.createCriteria("com.encryption.model.FileLog")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding FileLog instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from FileLog as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all FileLog instances");
		try {
			String queryString = "from FileLog";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public FileLog merge(FileLog detachedInstance) {
		log.debug("merging FileLog instance");
		try {
			FileLog result = (FileLog) getSession().merge(detachedInstance);
			log.debug("merge successful");
			getSession().flush();
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(FileLog instance) {
		log.debug("attaching dirty FileLog instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(FileLog instance) {
		log.debug("attaching clean FileLog instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/**
	 * 根据condition对日志表查询
	 * 
	 * @param instance
	 * @return
	 */
	public List<FileLog> findByExample(List<Condition> conditions) {
		log.debug("finding FileLog instance by example");
		try {

			Criteria criteria = getSession().createCriteria(FileLog.class);

			// 设置查询条件
			BaseHibernateDAO.addCriteria(conditions, criteria);

			List<FileLog> results = criteria.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/**
	 * 分页查询日志表
	 */
	public List<FileLog> findByExample4Page(List<Condition> conditions,
			int page, int pageSize) {
		log.debug("finding FileLog instance by example");
		try {
			Criteria criteria = getSession().createCriteria(FileLog.class);
			// 设置查询条件
			BaseHibernateDAO.addCriteria(conditions, criteria);
			criteria.setFirstResult(page);
			criteria.setFetchSize(pageSize);
			List<FileLog> results = criteria.list();

			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	/**
	 * 分页查询文件日志表
	 */
	public List<FileLog> findPagingByCondition(List<Condition> conditions, int page, int pageSize) {
		try {
			int startIndex = page * pageSize;
			String condsql = FileLogConditionSqlParser.parse(conditions);
			
			String sql = "select * from file_log where 1=1 " + condsql + "limit "+ startIndex +", "+ pageSize; 
			
			Query query = getSession().createSQLQuery(sql).addEntity(FileLog.class);
			List<FileLog> results = query.list();
			
			log.debug("执行语句:" + sql);
			return results;
		} catch (RuntimeException re) {
			log.error("findPagingByCondition failed", re);
			throw re;
		}
	}
	
	/**
	 * 分页查询文件表
	 */
	public int findTotalCountByCondition(List<Condition> conditions) {
		try {
			String condsql = FileLogConditionSqlParser.parse(conditions);
			
			String sql = "select count(*) from file_log where 1=1 " + condsql;
			
			Query query = getSession().createSQLQuery(sql);
			int totalCount = ((BigInteger) query.list().get(0)).intValue();
			
			log.debug("执行语句:" + sql);
			return totalCount;
		} catch (RuntimeException re) {
			log.error("findTotalCountByCondition failed", re);
			throw re;
		}
	}

	private static FileLogDAO fileLogDAO = null;

	public static FileLogDAO getFileLogDAO() {
		if (null == fileLogDAO) {
			fileLogDAO = new FileLogDAO();
		}
		return fileLogDAO;
	}

}